package com.huanming.dsjc.modular.university.controller;

import com.huanming.dsjc.common.controller.BaseController;
import com.huanming.dsjc.core.util.ToolUtil;
import com.huanming.dsjc.modular.goods.entity.Group;
import com.huanming.dsjc.modular.university.dao.ClassMapper;
import com.huanming.dsjc.modular.university.dao.GroupMapper;
import com.huanming.dsjc.modular.university.dao.StudentMapper;
import com.huanming.dsjc.modular.university.entity.Classes;
import com.huanming.dsjc.modular.university.entity.Student;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 学生管理控制器
 *
 * @author xufei
 * @Date 2018-01-31 14:08:28
 */
@Controller
@RequestMapping("/student")
public class StudentController extends BaseController {

    private String PREFIX = "/university/student/";

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private GroupMapper groupMapper;

    @Autowired
    private ClassMapper classMapper;
    /**
     * 跳转到学生管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "student.html";
    }

    /**
     * 跳转到添加学生管理
     */
    @RequestMapping("/student_add")
    public String studentAdd(Model model) {
        List<Classes> classlist = this.classMapper.listClasses("");
        model.addAttribute("classes",classlist);
        List<Group> groupList = this.groupMapper.list("");
        model.addAttribute("groups",groupList);
        return PREFIX + "student_add.html";
    }

    /**
     * 跳转到修改学生管理
     */
    @RequestMapping("/student_update/{studentId}")
    public String studentUpdate(@PathVariable Integer studentId, Model model) {
        List<Classes> classlist = this.classMapper.listClasses("");
        model.addAttribute("classes",classlist);
        List<Group> groupList = this.groupMapper.list("");
        model.addAttribute("groups",groupList);
        Student student = studentMapper.selectById(studentId);
        model.addAttribute("student",student);
        return PREFIX + "student_edit.html";
    }

    /**
     * 获取学生管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Student> list = this.studentMapper.list(condition);
        for (Student student:list){
            if(ToolUtil.isNotEmpty(student.getClassId())){
                 Classes classes = this.classMapper.selectById(student.getClassId());
                 if(ToolUtil.isNotEmpty(classes)){
                     student.setClassName(classes.getClassName());
                 }
                 Group group = this.groupMapper.selectById(student.getGroupId());
                 if(ToolUtil.isNotEmpty(group)){
                     student.setGroupName(group.getGroupName());
                 }
            }
        }
        return list;
    }

    /**
     * 新增学生管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Student student) {
        this.studentMapper.insert(student);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除学生管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer studentId) {
        this.studentMapper.deleteById(studentId);
        return SUCCESS_TIP;
    }


    /**
     * 修改学生管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Student student) {
        this.studentMapper.updateById(student);
        return super.SUCCESS_TIP;
    }

    /**
     * 学生管理详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
